﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Aandelendelen.Mail;
using Aandelendelen.Db;
using Aandelendelen.Obs;
using System.Xml;

namespace Aandelendelen.Daily
{
	public class BirthDay : DailyJob
	{
		public BirthDay(XmlNode jobNode)
			: base(jobNode, "Sending birthday messages...")
		{ }

		public override void Execute()
		{
			this.OnStart();

			try
			{
				this.Handle();
			}
			catch (Exception e)
			{
				this.OnError(e);
			}

			this.OnEnd();
		}

		private void Handle()
		{
			Query query = new Query("s_birthday");

			ResultSet resultSet = (ResultSet)query.Execute();

			int count = resultSet.Count;

			for (int i = 0; i < count; i++)
			{
				Userinfo userInfo = new Userinfo(resultSet, i, false);

				MailArguments mailArgs = MailArguments.MailArgs;
				mailArgs.Clear();

				mailArgs["{aanhef}"] = userInfo.MailAanhef;

				EMail mail = MailCache.GetMail("happybirthday");
				mail.Recipients = userInfo.Email;

				emails.Add(mail);
			}
		}
	}
}